package com.three.util;

import net.sf.json.JSONObject;

import org.apache.log4j.Logger;

import cn.jiguang.common.resp.APIConnectionException;
import cn.jiguang.common.resp.APIRequestException;
import cn.jpush.api.JPushClient;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Message;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.notification.AndroidNotification;
import cn.jpush.api.push.model.notification.Notification;

import com.three.controller.UserController;

public class JPushUtil {

	private static Logger logger = Logger.getLogger(UserController.class);

	/**
	 * 推送公告通知
	 * @param tag
	 * @param alert
	 * @param title
	 */
	public static void createPushAnnounceMsg(String tag, String alert, String title) {
		JPushClient jpushClient = new JPushClient(Constant.JPUSH_MASTER_SECRET,
				Constant.JPUSH_APP_KEY);
		// For push, all you need do is to build PushPayload object.
		PushPayload payload = buildPushObject_android_tag_alertWithTitle(tag,
				alert, title);

		try {
			PushResult result = jpushClient.sendPush(payload);
			logger.info("Got result - " + result);
		} catch (APIConnectionException e) {
			// Connection error, should retry later
			logger.info("Connection error, should retry later", e);

		} catch (APIRequestException e) {
			// Should review the error, and fix the request
			logger.error("Should review the error, and fix the request", e);
			logger.info("HTTP Status: " + e.getStatus());
			logger.info("Error Code: " + e.getErrorCode());
			logger.info("Error Message: " + e.getErrorMessage());
		}
	}

	/**
	 * 推送考勤通知
	 * @param tag
	 * @param alert
	 * @param title
	 */
	public static void createPushRollcallMsg(String tag, String alert, String title) {
		JPushClient jpushClient = new JPushClient(Constant.JPUSH_MASTER_SECRET,
				Constant.JPUSH_APP_KEY);
		// For push, all you need do is to build PushPayload object.
		JSONObject obj = new JSONObject();
		obj.put("code", "rollcall");
		obj.put("type", Constant.JPUSH_TEC_STU);
		PushPayload payload = 
				buildPushObject_android_tag_alertWithTitle_message(tag, alert, title, obj.toString());
		try {
			PushResult result = jpushClient.sendPush(payload);
			logger.info("Got result - " + result);
		} catch (APIConnectionException e) {
			// Connection error, should retry later
			logger.info("Connection error, should retry later", e);

		} catch (APIRequestException e) {
			// Should review the error, and fix the request
			logger.error("Should review the error, and fix the request", e);
			logger.info("HTTP Status: " + e.getStatus());
			logger.info("Error Code: " + e.getErrorCode());
			logger.info("Error Message: " + e.getErrorMessage());
		}
	}
	
	/**
	 * 更新考勤状态数字
	 * @param tag
	 */
	public static void updateRollcallNum(String tag) {
		JPushClient jpushClient = new JPushClient(Constant.JPUSH_MASTER_SECRET,
				Constant.JPUSH_APP_KEY);
		// For push, all you need do is to build PushPayload object.
		JSONObject obj = new JSONObject();
		obj.put("code", "rollcall");
		obj.put("type", Constant.JPUSH_STU_TEC);
		PushPayload payload = 
				buildPushObject_android_tag_message(tag, obj.toString());

		try {
			PushResult result = jpushClient.sendPush(payload);
			logger.info("Got result - " + result);
		} catch (APIConnectionException e) {
			// Connection error, should retry later
			logger.info("Connection error, should retry later", e);

		} catch (APIRequestException e) {
			// Should review the error, and fix the request
			logger.error("Should review the error, and fix the request", e);
			logger.info("HTTP Status: " + e.getStatus());
			logger.info("Error Code: " + e.getErrorCode());
			logger.info("Error Message: " + e.getErrorMessage());
		}
	}
	
	private static PushPayload buildPushObject_android_tag_alertWithTitle(
			String tag, String alert, String title) {
		return PushPayload.newBuilder().setPlatform(Platform.android())
				.setAudience(Audience.tag_and(tag, "stu"))
				.setNotification(Notification.android(alert, title, null))
				.build();
	}
	
	private static PushPayload buildPushObject_android_tag_alertWithTitle_message(
			String tag, String alert, String title,String message) {
		return PushPayload.newBuilder().setPlatform(Platform.android())
				.setAudience(Audience.tag_and(tag, "stu"))
				.setNotification(Notification.newBuilder()
						.addPlatformNotification(AndroidNotification.newBuilder()
								.setAlert(alert)
								.setTitle(title)
								.build())
							.build())
				.setMessage(Message.content(message))
				.build();
	}
	
	private static PushPayload buildPushObject_android_tag_message(
			String tag, String message) {
		return PushPayload.newBuilder().setPlatform(Platform.android())
				.setAudience(Audience.tag_and(tag, "tec"))
				.setMessage(Message.content(message))
				.build();
	}

}
